All files / web/src/app/api/worksheets/sessions/[sessionId] route.ts

0% Statements 0/51
0% Branches 0/1
0% Functions 0/1
0% Lines 0/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                                                                                                       
import { eq } from 'drizzle-orm'
import { type NextRequest, NextResponse } from 'next/server'
import { db } from '@/db'
import { worksheetAttempts } from '@/db/schema'
import { withAuth } from '@/lib/auth/withAuth'

/**
 * GET /api/worksheets/sessions/[sessionId]
 *
 * Returns all worksheet attempts for a given session ID
 * Used by desktop to poll for new uploads from smartphone QR scan workflow
 */
export const GET = withAuth(async (_request, { params }) => {
  try {
    const { sessionId } = (await params) as { sessionId: string }

    if (!sessionId) {
      return NextResponse.json({ error: 'Missing sessionId' }, { status: 400 })
    }

    // Get all attempts for this session
    const attempts = await db
      .select()
      .from(worksheetAttempts)
      .where(eq(worksheetAttempts.sessionId, sessionId))
      .orderBy(worksheetAttempts.createdAt)

    return NextResponse.json({
      sessionId,
      count: attempts.length,
      attempts: attempts.map((attempt) => ({
        id: attempt.id,
        status: attempt.gradingStatus,
        uploadedAt: attempt.createdAt,
        totalProblems: attempt.totalProblems,
        correctCount: attempt.correctCount,
        accuracy: attempt.accuracy,
        suggestedStepId: attempt.suggestedStepId,
      })),
    })
  } catch (error) {
    console.error('Session fetch error:', error)
    return NextResponse.json(
      {
        error: 'Failed to fetch session uploads',
        details: error instanceof Error ? error.message : 'Unknown error',
      },
      { status: 500 }
    )
  }
})